<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

//名片功能
namespace app\controller;
use think\facade\Db;
class ApiMingpian extends ApiCommon
{	
	public function index(){
		$set = Db::name('mingpian_set')->where('aid',aid)->find();
		if(!$set || $set['status'] == 0) return json(['status'=>-4,'msg'=>'功能未启用']);

		if(input('param.scene') != 1154){ //不是朋友圈卡片的场景
			$this->checklogin();
		}
		$where = [];
        $where[] = ['aid', '=', aid];
		if(input('param.id')){
			$where[] = ['id', '=', input('param.id/d')];
		}else{
			$where[] = ['mid', '=', mid];
		}

		$info = Db::name('mingpian')->where($where)->find();
		if(!$info){
			if(input('param.id')){
				return json(['status'=>-4,'msg'=>'名片不存在','url'=>'/pagesExt/mingpian/edit']);
			}else{
				return json(['status'=>-4,'msg'=>'请先创建名片','url'=>'/pagesExt/mingpian/edit']);
			}
		}
		$pagecontent = json_decode(\app\common\System::initpagecontent($info['detail'],aid),true);
		if(!$pagecontent) $pagecontent = [];
		
		$field_list = json_decode($set['field_list'],true);

		$newfield_list = [];
		$field_list2 = [];
		$i = 0;
		foreach($field_list as $k=>$v){
			$i++;
			if($i <= 3){
				$field_list2[$k] = $v;
			}
			if($v['isshow'] == 1){
				$newfield_list[$k] = $v;
			}
		}
		$viewmymp = false;
		if($this->member && $info['mid'] != mid){
			Db::name('mingpian_readlog')->where('aid',aid)->where('mid',mid)->where('mpid',$info['id'])->delete();
			$data = [];
			$data['aid'] = aid;
			$data['mid'] = mid;
			$data['mpid'] = $info['id'];
			$data['createtime'] = time();
			Db::name('mingpian_readlog')->insert($data);

			$hasmp = Db::name('mingpian')->where('aid',aid)->where('mid',mid)->find();
			if($hasmp){
				$viewmymp = true;
			}
		}

		$rdata = [];
		$rdata['info'] = $info;
		$rdata['mid'] = mid;
		$rdata['pagecontent'] = $pagecontent;
		$rdata['field_list'] = $newfield_list;
		$rdata['field_list2'] = $field_list2;
		$rdata['viewmymp'] = $viewmymp;
		return $this->json($rdata);
	}
	//谁看过
	public function readlog(){
		$this->checklogin();
		$id = input('param.id/d');
		$info = Db::name('mingpian')->where('id',$id)->find();
		if(!$info || $info['mid'] != mid) return json(['status'=>-4,'msg'=>'无权限查看']);
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mpid','=',$id];
		$datalist = Db::name('mingpian_readlog')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $k=>$v){
			$memberinfo = Db::name('member')->where('id',$v['mid'])->field('headimg,nickname')->find();
			$datalist[$k]['headimg'] = $memberinfo['headimg'];
			$datalist[$k]['nickname'] = $memberinfo['nickname'];
		}

        return $this->json(['status'=>1,'data'=>$datalist]);
	}
	//谁收藏了
	public function favoritelog(){
		$this->checklogin();
		$id = input('param.id/d');
		$info = Db::name('mingpian')->where('id',$id)->find();
		if(!$info || $info['mid'] != mid) return json(['status'=>-4,'msg'=>'无权限查看']);
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mpid','=',$id];
		$datalist = Db::name('mingpian_favorite')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $k=>$v){
			$memberinfo = Db::name('member')->where('id',$v['mid'])->field('headimg,nickname')->find();
			$datalist[$k]['headimg'] = $memberinfo['headimg'];
			$datalist[$k]['nickname'] = $memberinfo['nickname'];
		}
        return $this->json(['status'=>1,'data'=>$datalist]);
	}
	//编辑名片
	public function edit(){
		$this->checklogin();
		$set = Db::name('mingpian_set')->where('aid',aid)->find();
		if(!$set || $set['status'] == 0) return json(['status'=>-4,'msg'=>'功能未启用']);

		
		$createtj = explode(',',$set['createtj']);
		if(!in_array('-1',$createtj) && !in_array($this->member['levelid'],$createtj)){ //不是所有人
			return $this->json(['status'=>-4,'msg'=>'您没有权限创建名片']);
		}

		$info = Db::name('mingpian')->where('aid',aid)->where('mid',mid)->find();
		if(!$info) $info = [];

		$bglist = $set['bgpics'] ? explode(',',$set['bgpics']) : [];
		
		$pagecontent = json_decode(\app\common\System::initpagecontent($info['detail'],aid),true);
		if(!$pagecontent) $pagecontent = [];

		$field_list = json_decode($set['field_list'],true);

		$rdata = [];
		$rdata['info'] = $info;
		$rdata['bglist'] = $bglist;
		$rdata['field_list'] = $field_list;
		$rdata['pagecontent'] = $pagecontent;
		return $this->json($rdata);
	}
	//保存名片
	public function save(){
		$this->checklogin();
		$mingpian = Db::name('mingpian')->where('aid',aid)->where('mid',mid)->find();
		$info = input('post.info/a');
		$data = array();
		$data['aid'] = aid;
		$data['mid'] = mid;
		$data['bgpic'] = $info['bgpic'];
		$data['headimg'] = $info['headimg'];
		$data['realname'] = $info['realname'];
		$touxianArr = [];
		if($info['touxian1']) $touxianArr[] = $info['touxian1'];
		if($info['touxian2']) $touxianArr[] = $info['touxian2'];
		if($info['touxian3']) $touxianArr[] = $info['touxian3'];

		$data['touxian1'] = $touxianArr[0];
		$data['touxian2'] = $touxianArr[1] ?? '';
		$data['touxian3'] = $touxianArr[2] ?? '';
		$data['tel'] = $info['tel'];
		$data['weixin'] = $info['weixin'];
		$data['address'] = $info['address'];
		$data['email'] = $info['email'];
		$data['douyin'] = $info['douyin'];
		$data['weibo'] = $info['weibo'];
		$data['toutiao'] = $info['toutiao'];
		$data['field1'] = $info['field1'] ?? '';
		$data['field2'] = $info['field2'] ?? '';
		$data['field3'] = $info['field3'] ?? '';
		$data['field4'] = $info['field4'] ?? '';
		$data['field5'] = $info['field5'] ?? '';
		$data['sharetitle'] = $info['sharetitle'] ?? '';
		$data['longitude'] = $info['longitude'] ?? '';
		$data['latitude'] = $info['latitude'] ?? '';
		
		$data['detail'] = json_encode(input('post.pagecontent'));
		
		if(!$mingpian){
			$data['createtime'] = time();
		}
		$data['updatetime'] = time();

		if($mingpian){
			Db::name('mingpian')->where('aid',aid)->where('id',$mingpian['id'])->update($data);
		}else{
			$data['aid'] = aid;
			$proid = Db::name('mingpian')->insertGetId($data);
		}
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//删除
	public function del(){
		$this->checklogin();
		$id = input('post.id/d');
		$rs = Db::name('mingpian')->where('aid',aid)->where('mid',mid)->delete();
		return $this->json(['status'=>1,'msg'=>'操作成功']);
	}

	//存名片夹
	public function addfavorite(){
		$this->checklogin();
		$id = input('param.id/d');
		$info = Db::name('mingpian')->where('aid',aid)->where('id',$id)->find();
		if(!$info){
			return json(['status'=>0,'msg'=>'未找到该名片']);
		}
		Db::name('mingpian_favorite')->where('aid',aid)->where('mid',mid)->where('mpid',$id)->delete();
		$data = [];
		$data['aid'] = aid;
		$data['mid'] = mid;
		$data['mpid'] = $info['id'];
		$data['createtime'] = time();
		Db::name('mingpian_favorite')->insert($data);
		return json(['status'=>1,'msg'=>'保存成功']);
	}
	//删名片夹
	public function delfavorite(){
		$this->checklogin();
		$id = input('param.id/d');
		Db::name('mingpian_favorite')->where('aid',aid)->where('mid',mid)->where('id',$id)->delete();
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//我的名片夹
	public function favorite(){
		$this->checklogin();
		$pagenum = input('post.pagenum');
		if(!$pagenum) $pagenum = 1;
		$pernum = 20;
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['mid','=',mid];
		$datalist = Db::name('mingpian_favorite')->where($where)->page($pagenum,$pernum)->order('id desc')->select()->toArray();
		if(!$datalist) $datalist = [];
		foreach($datalist as $k=>$v){
			$datalist[$k]['info'] = Db::name('mingpian')->where('id',$v['mpid'])->find();
		}
		if($pagenum == 1){
			$set = Db::name('mingpian_set')->where('aid',aid)->find();
			$field_list = json_decode($set['field_list'],true);
			
			$newfield_list = [];
			$field_list2 = [];
			$i = 0;
			foreach($field_list as $k=>$v){
				$i++;
				if($i <= 3){
					$field_list2[$k] = $v;
				}
				if($v['isshow'] == 1){
					$newfield_list[$k] = $v;
				}
			}
		}
        return $this->json(['status'=>1,'data'=>$datalist,'field_list'=>$newfield_list,'field_list2'=>$field_list2]);
	}
}